using System.Web.UI;
using System.Xml;
using Coffee.DataMappers;

namespace Coffee
{
    public class SQBindingField
    {
        public string ControlType;
        public string Target;
        public string PropertyName;
    }

    public class SQBindingContext
    {
        public SQBindingField Current;
        public SQBindingField Previous;
        XmlNode configuration;

        public SQBindingContext(XmlNode configuration)
        {
            this.configuration = configuration;
            Current = GetBindingField(configuration, "current");
            Previous = GetBindingField(configuration, "previous");
        }

        public void ReadProperties(IDataMapper mapper, Control previous, Control current)
        {
            Reflection.SetProperty(
                Previous.Target,
                previous,
                mapper.Previous.GetProperty(Previous.PropertyName)
            );

            Reflection.SetProperty(
                Current.Target,
                current,
                mapper.Current.GetProperty(Current.PropertyName)
            );
        }

        public void WriteProperties(IDataMapper mapper, object current)
        {
            XmlAttribute dataType;
            bool IsNullOrEmpty = false;
            string curs = string.Empty;

            if (current.GetType() == typeof(string))
            {
                curs = (string)current;
                IsNullOrEmpty = string.IsNullOrEmpty(curs);
            }

            if ((dataType = configuration.Attributes["datatype"]) != null)
            {
                if (dataType.Value == "integer")
                {
                    if (current.GetType() == typeof(string))
                    {
                        if (IsNullOrEmpty)
                        {
                            mapper.Current.SetProperty(Current.PropertyName, null);
                        }
                        else
                        {
                            mapper.Current.SetProperty(Current.PropertyName, int.Parse(curs.Trim()));
                        }
                    }
                    else
                    {
                        mapper.Current.SetProperty(Current.PropertyName, current);
                    }
                }
                else if (dataType.Value == "decimal")
                {
                    if (current.GetType() == typeof(string))
                    {
                        if (IsNullOrEmpty)
                        {
                            mapper.Current.SetProperty(Current.PropertyName, null);
                        }
                        else
                        {
                            mapper.Current.SetProperty(Current.PropertyName, decimal.Parse(curs.Trim()));
                        }
                    }
                    else
                    {
                        mapper.Current.SetProperty(Current.PropertyName, current);
                    }
                }
                else
                {
                    if (IsNullOrEmpty)
                    {
                        mapper.Current.SetProperty(Current.PropertyName, null);
                    }
                    else
                    {
                        mapper.Current.SetProperty(Current.PropertyName, current);
                    }
                }
            }
            else
            {
                if (IsNullOrEmpty)
                {
                    mapper.Current.SetProperty(Current.PropertyName, null);
                }
                else
                {
                    mapper.Current.SetProperty(Current.PropertyName, current);
                }
            }
        }


        SQBindingField GetBindingField(XmlNode configuration, string name)
        {
            SQBindingField field = new SQBindingField();
            string ctlId = configuration.Attributes["id"].Value;
            string ctlType = configuration.Attributes["type"].Value;
            string defaultType = string.Empty;

            switch (name)
            {
                case "current":
                    switch (ctlType)
                    {
                        case "TextBox": defaultType = "Text"; break;
                        case "DropDownList": defaultType = "SelectedValue"; break;
                    }
                    break;
                case "previous":
                    switch (ctlType)
                    {
                        case "TextBox": defaultType = "Text"; break;
                        case "DropDownList": defaultType = "Text"; break;
                    }
                    break;
            }

            XmlNode binding = configuration.SelectSingleNode("binding/" + name);
            if (binding != null)
            {
                if (binding.Attributes["type"] != null)
                {
                    field.Target = binding.Attributes["type"].Value;
                }
                else
                {
                    field.Target = defaultType;
                }
                field.PropertyName = binding.SelectSingleNode("text()").Value;
            }
            else
            {
                field.Target = defaultType;
                field.PropertyName = ctlId;
            }

            return field;
        }

    }
}